package com.mulanweb.api.module.controller.wechat;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.mulanweb.api.module.model.wechat.Menu;
import com.mulanweb.db.common.Constant;
import com.mulanweb.handler.wechat.TokenHandler;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/11.
 */
public class MenuController extends Controller{

    /**
     * 未使用，关闭中
     */
    public void createMenu() throws IOException {
        List<Menu> wxs = new ArrayList<Menu>(3);
        List<Menu> subWxs = new ArrayList<Menu>(5) ;

        Menu menu1 = new Menu();
       /* menu1.setId(1);
        menu1.setName("特价专区");*/

        Menu menu2 = new Menu();
        menu2.setId(11);
        menu2.setName("爆款推荐");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=hot_commodity#wechat_redirect");
       // subWxs.add(menu2);

        /*menu2 = new Menu() ;
        menu2.setId(12);
        menu2.setName("优惠券");
        menu2.setType("click");
        menu2.setKey("coupon");
        subWxs.add(menu2);*/

        //menu1.setSub_button(subWxs);
        wxs.add(menu2);

        menu1 = new Menu();
        menu1.setId(2);
        menu1.setName("现在预定");

        menu2 = new Menu() ;
        menu2.setId(21);
        menu2.setName("门票折券");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=ticket#wechat_redirect");

        subWxs = new ArrayList<Menu>(5) ;
        subWxs.add(menu2);

        /*menu2 = new Menu() ;
        menu2.setId(22);
        menu2.setName("包车陪游");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=chartered#wechat_redirect");
        subWxs.add(menu2);*/


    /*    menu2 = new Menu() ;
        menu2.setId(22);
        menu2.setName("签证办理");
        menu2.setType("click");
        menu2.setKey("chartered");
        subWxs.add(menu2);*/

        menu2 = new Menu() ;
        menu2.setId(21);
        menu2.setName("签证办理");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=visa#wechat_redirect");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(22);
        menu2.setName("包车陪游");
        menu2.setType("click");
        menu2.setKey("chartered");
        subWxs.add(menu2);

        menu1.setSub_button(subWxs);

        wxs.add(menu1);

        menu1 = new Menu();
        menu1.setId(3);
        menu1.setName("个人中心");

        menu2 = new Menu() ;
        menu2.setId(31);
        menu2.setName("订单列表");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=order#wechat_redirect");

        subWxs = new ArrayList<Menu>(5) ;
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(32);
        menu2.setName("收藏列表");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1a5d74a8c7cb135e&redirect_uri=http%3a%2f%2fweixin.mulanweb.com%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=collection#wechat_redirect");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(33);
        menu2.setName("摄影大赛");
        menu2.setType("view");
        menu2.setUrl("https://sojump.com/jq/13998242.aspx");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(32);
        menu2.setName("优惠券");
        menu2.setType("click");
        menu2.setKey("coupon");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(32);
        menu2.setName("客服中心");
        menu2.setType("click");
        menu2.setKey("service");
        subWxs.add(menu2);

        menu1.setSub_button(subWxs);
        wxs.add(menu1);

        Map<String,Object> tt = new HashMap<String,Object>();
        tt.put("button",wxs) ;
        String para = JSON.toJSONString(tt) ;
        System.out.println(para);

        /* 开始关闭
        CloseableHttpClient client = HttpClients.createDefault();

        String url = Constant.WeChatFinalValue.getCreateMenuUrl(TokenHandler.getAccessToken());
        //String url = Constant.WeChatFinalValue.CREATE_MENU_URL.replace("ACCESS_TOKEN","dK1e3bUXwCE7L2hYuE8HYmstQ6WvNP4VONXnxBNDe7NqGekk-N-KWXHn5vgAgZJwsH1YL7PbIaJ-sS3VrblEtp-CaKEyJygS6hT-_1oD-p1t1u7df-2ZuSR4Q78NeZ5BCSLaAHAWMF");
        HttpPost post = new HttpPost(url) ;
        post.addHeader("Content/Type","application/json");
        StringEntity stringEntity = new StringEntity(para, ContentType.create("application/json","UTF-8")) ;
        post.setEntity(stringEntity);
        CloseableHttpResponse resp = client.execute(post) ;

        int returnCode = resp.getStatusLine().getStatusCode() ;
        if(200<=returnCode && 300>=returnCode){
            System.out.println(EntityUtils.toString(resp.getEntity()));
        }*/
    }

   /** 注释中......
    public void createMenuTestAccount() throws IOException {
        List<Menu> wxs = new ArrayList<Menu>(3);
        List<Menu> subWxs = new ArrayList<Menu>(5) ;

        Menu menu1 = new Menu();
        menu1.setId(1);
        menu1.setName("特价专区");

        Menu menu2 = new Menu();
        menu2.setId(11);
        menu2.setName("爆款推荐");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx323bd2d11b36dd8d&redirect_uri=http%3a%2f%2fbluefly.51vip.biz%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=hot_commodity#wechat_redirect");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(12);
        menu2.setName("优惠券");
        menu2.setType("click");
        menu2.setKey("coupon");
        subWxs.add(menu2);

        menu1.setSub_button(subWxs);
        wxs.add(menu1);

        menu1 = new Menu();
        menu1.setId(2);
        menu1.setName("现在预定");

        menu2 = new Menu() ;
        menu2.setId(21);
        menu2.setName("门票券票");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx323bd2d11b36dd8d&redirect_uri=http%3a%2f%2fbluefly.51vip.biz%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=ticket#wechat_redirect");

        subWxs = new ArrayList<Menu>(5) ;
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(22);
        menu2.setName("包车陪游");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx323bd2d11b36dd8d&redirect_uri=http%3a%2f%2fbluefly.51vip.biz%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=chartered#wechat_redirect");
        subWxs.add(menu2);

        menu1.setSub_button(subWxs);

        wxs.add(menu1);

        menu1 = new Menu();
        menu1.setId(3);
        menu1.setName("个人中心");

        menu2 = new Menu() ;
        menu2.setId(31);
        menu2.setName("订单列表");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx323bd2d11b36dd8d&redirect_uri=http%3a%2f%2fbluefly.51vip.biz%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=order#wechat_redirect");

        subWxs = new ArrayList<Menu>(5) ;
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(32);
        menu2.setName("收藏列表");
        menu2.setType("view");
        menu2.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx323bd2d11b36dd8d&redirect_uri=http%3a%2f%2fbluefly.51vip.biz%2fmulan_xxl%2fadmin%2ftest%2fgetAuthority&response_type=code&scope=snsapi_base&state=collection#wechat_redirect");
        subWxs.add(menu2);

        menu2 = new Menu() ;
        menu2.setId(32);
        menu2.setName("客服中心");
        menu2.setType("click");
        menu2.setKey("service");
        subWxs.add(menu2);

        menu1.setSub_button(subWxs);
        wxs.add(menu1);

        Map<String,Object> tt = new HashMap<String,Object>();
        tt.put("button",wxs) ;
        String para = JSON.toJSONString(tt) ;
        System.out.println(para);

        CloseableHttpClient client = HttpClients.createDefault();

        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=2qOOuGVaYlAfCkR9Gm1CAfkuEn2OsDWmj9ZjC1I_yPUSxAJk1TNPH1UD746YFxJQOTk1S9P0ck7odrJ72DHCpHLVLtP7b3vDXlZOd5yP_rWq5KnkttLaVRaCoo4HLwvtSIDcAIAEUP";
        //String url = Constant.WeChatFinalValue.CREATE_MENU_URL.replace("ACCESS_TOKEN","dK1e3bUXwCE7L2hYuE8HYmstQ6WvNP4VONXnxBNDe7NqGekk-N-KWXHn5vgAgZJwsH1YL7PbIaJ-sS3VrblEtp-CaKEyJygS6hT-_1oD-p1t1u7df-2ZuSR4Q78NeZ5BCSLaAHAWMF");
        HttpPost post = new HttpPost(url) ;
        post.addHeader("Content/Type","application/json");
        StringEntity stringEntity = new StringEntity(para, ContentType.create("application/json","UTF-8")) ;
        post.setEntity(stringEntity);
        CloseableHttpResponse resp = client.execute(post) ;

        int returnCode = resp.getStatusLine().getStatusCode() ;
        if(200<=returnCode && 300>=returnCode){
            System.out.println(EntityUtils.toString(resp.getEntity()));
        }
    }*/
}
